package node;

import java.util.LinkedList;

public class Worm {
    LinkedList<Node> nodes = new LinkedList<Node> ();//蛇
    public static final int up = -10;//四个方向
    public static final int down = 10;
    public static final int left =-1;
    public static final int right=1;
    private int dir;//方向
    public Worm (){//对蛇初始化(给LinkedList<Node> nodes存入元素)并设定移动方向
        nodes. add (new Node (3,9));
        nodes.add (new Node (4, 9) );
        nodes. add (new Node (5, 9));
//        nodes. add (new Node (5, 10) );
//        nodes. add (new Node (5, 11) );
//        nodes. add (new Node (6, 11) );
//        nodes. add (new Node (7,11) );
        dir=right;
    }
    public Worm (LinkedList<Node> nodes){
        this.nodes.clear ();
        this.nodes.addAll(nodes);
    }

    //向某个方向移动一次的方法(给dir赋值，并调用一次step方法)
    public void step (int dir){
        if (this.dir+dir==0){
            throw new RuntimeException("不能掉头行驶 ");
        }
        this.dir=dir;
        step();
    }

    //移动一下的方法(删除最后一个Node，给最前边添加一个Node)
    public void step (){
        Node n = nodes.getFirst ();
        int i = n.getI() +dir/10;
        int j = n.getJ() +dir%10;
        System.out.println(i+"---"+j);
        n = new Node (i, j);
        nodes.addFirst(n);
        nodes.removeLast();
    }

    //判断蛇是否吃到食物的方法(是否包含食物的坐标i，j)
    public boolean contains (int i, int j){
        return nodes. contains (new Node (i,j));
    }

    public String toString(){
        return nodes. toString();
    }
}
